import java.util.ArrayList;

import structure.bigProdConnection;
import structure.bigProduction;
import structure.enumerateGrammar;
import structure.grammar;
import structure.item;
import structure.production;
import algorithm.algorithm;


public class Main {

	public static void main(String[] args) {
		
		String grammar= "A->BA|e;B->aB|b.";
		//"E->E+T|T;T->TxF|F;F->(E)|a.";"S->BBa;B->Bb|c.";"S->CC;C->cC|d.";"dcd$";
		//"E->E+T|TxF|(E)|a;T->TxF|(E)|a;F->(E)|a.";//"A->BA|e;B->aB|b.";"aabb$";
		////"S->aA|bB;A->e|cAd;B->e.";//"S->SSA|e;A->AAa|e|AB;B->b|bB|A.";
		//"E->E+T|T;T->TxF|F;F->(E)|a.";//"S->SSA|e;A->C|AAa|e|AB;B->b|bB|A;C->A|c.";
		//"S->CC;C->cC;C->d.";"dcd$";//"A->BA;A->e;B->aB;B->b.";"aabb$";
		//"E->E+T|T;T->T*a|a.";"a*a+a$";//"S->BBa;B->Bb|c.";//"ccbbba$";

		grammar g= new grammar(grammar);
		System.out.println("==================GRAMMAR==================\n");
		System.out.println(grammar);
		g.printItAll();
		//semplificazione
		g= algorithm.FromBelowProcedure(g);
		g= algorithm.FromAboveProcedure(g);
		System.out.println("\n-----------------Dopo le procedure di semplificazione-----------------");
		g.printItAll();
		//input word
		String w= "aabb$";
		System.out.println("La word in input è: " + w);
		//enumero la grammatica
		System.out.println("\n-----------------La grammatica enumerata è-----------------");
		enumerateGrammar e= new enumerateGrammar(g);
		e.printItAllIPF();
		
		System.out.println("\nnullable= " + e.getNullable() + "\n");
		
		//======================================================
		bigProdConnection bigP= new bigProdConnection(e);
		
		bigP.createLookahead();
		
		bigP.createMap();
		
		bigP.connect();
		
		bigP.createPowerset();
		
		bigP.createParsingTable();
		//======================================================
		
		bigP.printItAll();
		
		System.out.println("==================MAP==================\n");
		bigP.printMap();
		System.out.println("");
		
		System.out.println("==================CONNECTION==================\n");
		bigP.printConnections();
		System.out.println("");
		

		System.out.println("==================POWERSET==================\n");
		bigP.printPowerset();
		System.out.println("");
		
		System.out.println("==================MAP_POWERSET==================\n");
		bigP.printMapPowerset();
		System.out.println("");
		
		System.out.println("==================CONNECTION_POWERSET==================\n");
		bigP.printConnectionsPowerset();
		System.out.println("");
		
		System.out.println("==================PARSING_TABLE==================\n");
		if(!bigP.everythingOkInPT()){
			bigP.printError();
			System.out.println("La grammatica data non è una grammatica LR(1).");
			bigP.printParsingTable();
		}
		else{

			bigP.printParsingTable();
			System.out.println("La grammatica data è una grammatica LR(1).");
		}
		System.out.println("");
		
		System.out.println("==================PARSE_INPUT_WORD==================\n");
		bigP.parseInputWord(w);
		System.out.println("");
		
		
		//System.out.println(algorithm.First1(e, "A", null, null));
		
		/*ArrayList<String> beta= new ArrayList<String>();
		ArrayList<String> afterDot= new ArrayList<String>();
		for(bigProduction big : bigP.getBigProductions()){
			
			for(item i : big.getItems()){
				
				afterDot.add(i.getAfterDot());
				beta.add(bigP.getBeta(i));
			}
		}
		System.out.println(afterDot);
		System.out.println(beta);*/

	}

}
